#include "StdAfx.h"
#include <string>
#include <Util/StringUtil.h>
#include <interface/data_op/table_wrapper.h>
#include "recv_file_record_op.h"

namespace aot{ namespace data{

recv_file_record_op_impl::recv_file_record_op_impl(void)
{
}

recv_file_record_op_impl::~recv_file_record_op_impl(void)
{
}

bool 
recv_file_record_op_impl::insert(const irecv_file_info* p, int* new_id)
{
    if (!g_db_user) return false;
	if ( p == NULL )
	{
		return false;
	}

    std::string str_file_name = p->file_name->c_str();
    std::string str_sender_id = p->sender_id->c_str();
    std::string str_sender_name = p->sender_name->c_str();
    std::string str_recv_time = p->recv_time->c_str();
    std::string str_file_path = p->file_path->c_str();

    remove_sin_quot(str_file_name);
    remove_sin_quot(str_sender_id);
    remove_sin_quot(str_sender_name);
    remove_sin_quot(str_recv_time);
    remove_sin_quot(str_file_path);

    std::string sql_fmt = 
        "insert into receive_file_record \
        (id,\
        size,\
        flag,\
        file_name,\
        sender_id,\
        sender_name, \
        receive_time,\
        file_path) \
        values\
        (%d, %d, %d, '%s', '%s', '%s', '%s' , '%s')";

    std::string sql_exe;
    int t_new_id = get_table_next_id("receive_file_record");

    sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(),
        t_new_id,
        p->size,
        p->flag,
        str_file_name.c_str(),
		str_sender_id.c_str(),
		str_sender_name.c_str(),
		str_recv_time.c_str(),
		str_file_path.c_str()
        );

	if ( new_id != NULL )
	{
		*new_id = t_new_id;
	}
    return g_db_user->exec(sql_exe.c_str());
}

bool 
recv_file_record_op_impl::del(int id)
{
    std::string sql_fmt = "delete from receive_file_record where id=%d";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), id);
    return g_db_user->exec(sql_exe.c_str());
}


bool 
recv_file_record_op_impl::update(const irecv_file_info* p)
{
    if (!g_db_user) return false;


	std::string str_file_name = p->file_name->c_str();
	std::string str_sender_id = p->sender_id->c_str();
	std::string str_sender_name = p->sender_name->c_str();
	std::string str_recv_time = p->recv_time->c_str();
	std::string str_file_path = p->file_path->c_str();

	remove_sin_quot(str_file_name);
	remove_sin_quot(str_sender_id);
	remove_sin_quot(str_sender_name);
	remove_sin_quot(str_recv_time);
	remove_sin_quot(str_file_path);

    std::string sql_fmt = 
        "update receive_file_record set \
        size=%d,\
        flag=%d,\
        file_name='%s',\
        sender_id='%s',\
        sender_name='%s',\
        receive_time='%s', \
        file_path='%s' \
        where id=%d";

    std::string sql_exe;
    sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(),
		p->size,
		p->flag,
		str_file_name.c_str(),
		str_sender_id.c_str(),
		str_sender_name.c_str(),
		str_recv_time.c_str(),
		str_file_path.c_str(),
        p->id
        );

    return g_db_user->exec(sql_exe.c_str());
}

bool 
recv_file_record_op_impl::read(int id, isqlite3_table** out)
{
    std::string sql_fmt = "select \
						    id,\
						    size,\
						    flag,\
						    file_name,\
						    sender_id,\
						    sender_name, \
						    receive_time,\
						    file_path \
                            from receive_file_record \
                            where id=%d";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), id);
    return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
}

bool 
recv_file_record_op_impl::read(int id, irecv_file_info* out)
{
    aot::data::table_helper t_table;
    bool bRet =false;
    if ( this->read(id, t_table) )
    {
        if ( t_table.get_rows_count() > 0 )
        {
            t_table.set_row_index(0);

            out->id = t_table.get_field_value_int("id");
            out->size = t_table.get_field_value_int("size");
            out->flag = t_table.get_field_value_int("flag");

            out->file_name->assign(t_table.get_field_value_str("file_name").c_str());
            out->sender_id->assign(t_table.get_field_value_str("sender_id").c_str());
            out->sender_name->assign(t_table.get_field_value_str("sender_name").c_str());
            out->recv_time->assign(t_table.get_field_value_str("receive_time").c_str());
            out->file_path->assign(t_table.get_field_value_str("file_path").c_str());

            bRet = true;
        }


    }
    return bRet;
}

bool 
recv_file_record_op_impl::read(int id, irecv_file_info** out)
{
    aot::data::irecv_file_info* obj = new aot::data::recv_file_info_impl();
    *out = obj;
    return this->read(id, obj);
}

int 
recv_file_record_op_impl::get_count_by_user_id(const char* user_id)
{
	if ( user_id == NULL || user_id[0] == 0 )
	{
		return 0;
	}

    int total;
    std::string sql_fmt;
    std::string str_user_id;
    std::string sql_exe;

    str_user_id = user_id;
    remove_sin_quot(str_user_id);

    sql_fmt = "select count(*) as t from receive_file_record where sender_id='%s';";
    sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), str_user_id.c_str());
   
    total = g_db_user->exec_scalar(sql_exe.c_str());
    return total;
}

bool 
recv_file_record_op_impl::get_page_records_by_user_id(const char* user_id, int count_per_page, int page_index, isqlite3_table** out)
{
    int total = get_count_by_user_id(user_id);
	if ( total == 0 )
	{
		return false;
	}

    int limit;
    int offset;

    this->cal_page_info(total, count_per_page, page_index, limit, offset);

    std::string sql_fmt;
    std::string str_user_id;
    std::string sql_exe;

	str_user_id = user_id;
	remove_sin_quot(str_user_id);

	sql_fmt = "select \
			   id,\
			   size,\
			   flag,\
			   file_name,\
			   sender_id,\
			   sender_name, \
			   receive_time,\
			   file_path \
			   from receive_file_record \
			   where sender_id = '%s' order by receive_time desc limit %d offset %d;";
 
    sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), str_user_id.c_str(), limit, offset);
    return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
}

int 
recv_file_record_op_impl::get_count_by_key( const char* sender_name, const char* file_name )
{
	int total = 0;
	std::string sql_fmt;
	std::string str_user_name;
	std::string str_file_name;
	std::string sql_exe;

	str_file_name = file_name;
	str_user_name = sender_name;
	remove_sin_quot(str_file_name);
	remove_sin_quot(str_user_name);
	str_file_name = "%" + str_file_name + "%";
	str_user_name = "%" + str_user_name + "%";
	
	if ( str_user_name.empty() )
	{
		if ( !str_file_name.empty() )
		{
			sql_fmt = "select count(*) as t from receive_file_record where file_name like '%s';";
			sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), str_file_name.c_str());
			total = g_db_user->exec_scalar(sql_exe.c_str());
		}
	}
	else
	{
		if ( str_file_name.empty() )
		{
			sql_fmt = "select count(*) as t from receive_file_record where sender_name like '%s';";
			sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), str_user_name.c_str());
			total = g_db_user->exec_scalar(sql_exe.c_str());
		}
		else
		{
			sql_fmt = "select count(*) as t from receive_file_record where sender_name like '%s' and file_name like '%s';";
			sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), str_user_name.c_str(), str_file_name.c_str());
			total = g_db_user->exec_scalar(sql_exe.c_str());
		}
	}
	
	return total;
}

bool 
recv_file_record_op_impl::get_page_records_by_key( const char* sender_name, const char* file_name, int count_per_page, int page_index, isqlite3_table** out )
{
	int total = get_count_by_key(sender_name, file_name);
	if ( total == 0)
	{
		return false;
	}

	int limit;
	int offset;

	this->cal_page_info(total, count_per_page, page_index, limit, offset);

	std::string sql_fmt;
	std::string str_user_name;
	std::string str_file_name;
	std::string sql_exe;

	str_user_name = sender_name;
	str_file_name = file_name;
	remove_sin_quot(str_user_name);
	remove_sin_quot(str_file_name);
	str_file_name = "%" + str_file_name + "%";	
	str_user_name = "%" + str_user_name + "%";

	if ( str_user_name.empty() )
	{
		if ( !str_file_name.empty() )
		{
			sql_fmt = "select * from receive_file_record where file_name like '%s' order by receive_time desc limit %d offset %d;";
			sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), str_file_name.c_str(), limit, offset);
			return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
		}
	}
	else
	{
		if ( str_file_name.empty() )
		{
			sql_fmt = "select * from receive_file_record where sender_name like '%s' order by receive_time desc limit %d offset %d;";
			sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), str_user_name.c_str(), limit, offset);
			return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
		}
		else
		{
			sql_fmt = "select * from receive_file_record where sender_name like '%s' and file_name like '%s' order by receive_time desc limit %d offset %d;";
			sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), str_user_name.c_str(), str_file_name.c_str(), limit, offset);
			return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
		}
	}

	return false;
}

int
recv_file_record_op_impl::get_all_count()
{
	std::string sql_exe;
	sql_exe = "select count(*) as t from receive_file_record";

	return g_db_user->exec_scalar(sql_exe.c_str());
}

bool 
recv_file_record_op_impl::get_page_records( int count_per_page, int page_index, isqlite3_table** out )
{
	int total = get_all_count();
	if ( total == 0)
	{
		return false;
	}

	int limit;
	int offset;

	this->cal_page_info(total, count_per_page, page_index, limit, offset);

	std::string sql_exe;
	std::string sql_fmt;

	sql_fmt = "select * from receive_file_record  order by receive_time desc  limit %d offset %d;";
	sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), limit, offset);

	return g_db_user->exec_and_get_record(sql_exe.c_str(), out);
}

}
} /* end namespace aot/data*/
